﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PefStudio.Services
{
    public class MessageService
    {
        public const string Contract = "PefStudio.Services.Message";

        public static MessageService Instance { get; set; }
        public IMessageService IMessageService { get; set; }

        static MessageService()
        {
            Instance = new MessageService();
        }

        public static void ShowException(Exception ex)
        {
            ShowException(ex, null);
        }

        public static void ShowError(string message)
        {
            LoggingService.Error(message);
            Instance.IMessageService.ShowError(message);
        }

        public static void ShowErrorFormatted(string formatstring, params object[] formatitems)
        {
            ShowError(Format(formatstring, formatitems));
        }

        public static void ShowException(Exception ex, string message)
        {
            LoggingService.Error(message, ex);
            LoggingService.Warn("Stack trace of last exception log:\n" + Environment.StackTrace);
            Instance.IMessageService.ShowException(ex, message);
        }

        public static void ShowWarning(string message)
        {
            LoggingService.Warn(message);
            Instance.IMessageService.ShowWarning(message);
        }

        public static void ShowWarningFormatted(string formatstring, params object[] formatitems)
        {
            ShowWarning(Format(formatstring, formatitems));
        }

        static string _defaultMessageBoxTitle = "MessageBox";
        public static string DefaultMessageBoxTitle
        {
            get { return _defaultMessageBoxTitle; }
            set { _defaultMessageBoxTitle = value; }
        }

        public static void ShowMessage(string message)
        {
            ShowMessage(message, DefaultMessageBoxTitle);
        }

        public static void ShowMessageFormatted(string formatstring, params object[] formatitems)
        {
            ShowMessage(Format(formatstring, formatitems));
        }

        public static void ShowMessageFormatted(string caption, string formatstring, params object[] formatitems)
        {
            ShowMessage(Format(formatstring, formatitems), caption);
        }

        public static void ShowMessage(string message, string caption)
        {
            LoggingService.Info(message);
            Instance.IMessageService.ShowMessage(message, caption);
        }

        static string Format(string formatstring, object[] formatitems)
        {
            try
            {
                return String.Format(formatstring, formatitems);
            }
            catch (FormatException ex)
            {
                LoggingService.Warn(ex);

                var b = new StringBuilder(formatstring);
                foreach (var formatitem in formatitems)
                {
                    b.Append("\nItem: ");
                    b.Append(formatitem);
                }
                return b.ToString();
            }
        }
    }
}
